<?php
    $dbtype = "mysql";
    $host = "localhost";
    $dbname = "mydemo";
    $username = "root";
    $password = "";

    $dsn = "{$dbtype}:host={$host};dbname={$dbname}";

    try {
        //方法二：$pdo = new PDO("{$dbtype}:host={$host};dbname={$dbname}", $username, $password);
        $pdo = new PDO($dsn, $username, $password);
        $pdo->query("set names utf8");

        //echo "连接成功";
    } catch(PDOException $e) {
        die("连接失败" . $e->getMessage());
    }

?>